package boxe.timer.view;

/**
 * This view display the list of mode and the related list of runs
 * by using a Jscroll List. We can configure or edit modes and runs
 * 
 * @author Julien Perriard
 * @date 18.05.2010
 *
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import boxe.timer.env.Propertie;
import boxe.timer.manager.ModeManager;

public class ModeManagerView extends JFrame implements ListSelectionListener{

	/*
	 * local variable 
	 */
	private JButton cmdEditMode, cmdNewMode, cmdDelMode, cmdNewRun, cmdDelRun,
					cmdEditRun, cmdCancel, cmdSave, cmdRunUp, cmdRunDown;
	
	private JPanel panMode,panRun, panBorder, panCmdMode, panCmdRun,panCmd;
	
	private JTextField txtTitle;;
	private ModeManager modeManager;
	private String ICON = "images/icon.jpeg";
	private JList modeList;
	private JList runList;
	private Box[] modeBox;

	/**
	 * @param modeManager
	 */
	public ModeManagerView(ModeManager modeManager) {
		
		//init instance
		this.modeManager = modeManager;
		panBorder = new JPanel(new BorderLayout());
		panMode =  new JPanel(new BorderLayout());
		panRun =  new JPanel(new BorderLayout());
		panCmdMode = new JPanel();
		panCmdRun = new JPanel();
		panCmd = new JPanel();
		modeBox= new Box[2];
		modeBox[0] = Box.createVerticalBox();
		modeBox[1] = Box.createVerticalBox();
		txtTitle = new JTextField();
		txtTitle.setText("hello");
		txtTitle.setPreferredSize(new Dimension(20, 20));
		
		//Mode List
	    modeList = new JList(modeManager.getModeDlm());
	    runList = new JList(modeManager.getRunDlm());
	    initList();
	    
	    JScrollPane scrollMode = new JScrollPane(modeList);
	    JPanel modePanel = new JPanel();
	    JScrollPane scrollRun = new JScrollPane(runList);
	    JPanel runPanel = new JPanel();
	    modePanel.add(scrollMode);
	    runPanel.add(scrollRun);
	    
	    panMode.add(modePanel,BorderLayout.EAST);
	    panRun.add(runPanel,BorderLayout.WEST);
	    
	    //Command
	    cmdEditMode = new JButton("Edit");
	    cmdEditMode.setPreferredSize(Propertie.dimCmdBut());
	    cmdNewMode = new JButton("New");
	    cmdDelMode = new JButton("Remove");
	    cmdNewRun = new JButton("New");
	    cmdDelRun = new JButton("Remove");
		cmdEditRun = new JButton("Edit");
		cmdCancel = new JButton("Cancel");
		cmdSave = new JButton("Save");
		cmdRunUp = new JButton("Up");
		cmdRunDown = new JButton("Down");
		cmdEditMode.setActionCommand(Propertie.EDIT_MODE);
		cmdNewMode.setActionCommand(Propertie.NEW_MODE);
		cmdDelMode.setActionCommand(Propertie.DEL_MODE);
		cmdEditRun.setActionCommand(Propertie.EDIT_RUN);
		cmdNewRun.setActionCommand(Propertie.NEW_RUN);
		cmdDelRun.setActionCommand(Propertie.DEL_RUN);
		cmdRunUp.setActionCommand(Propertie.UP_RUN);
		cmdRunDown.setActionCommand(Propertie.DOWN_RUN);
		cmdCancel.setActionCommand(Propertie.CANCEL);
		cmdSave.setActionCommand(Propertie.SAVE);
		cmdEditRun.addActionListener(modeManager);
		cmdNewRun.addActionListener(modeManager);
		cmdDelMode.addActionListener(modeManager);
		cmdRunUp.addActionListener(modeManager);
		cmdRunDown.addActionListener(modeManager);
		cmdEditMode.addActionListener(modeManager);
		cmdNewMode.addActionListener(modeManager);
		cmdDelRun.addActionListener(modeManager);
		cmdCancel.addActionListener(modeManager);
		cmdEditRun.addActionListener(modeManager);
		cmdSave.addActionListener(modeManager);
		modeBox[0].add(cmdNewMode);
		modeBox[0].add(cmdEditMode);
		modeBox[0].add(cmdDelMode);
		modeBox[1].add(cmdNewRun);
		modeBox[1].add(cmdEditRun);
		modeBox[1].add(cmdRunUp);
		modeBox[1].add(cmdRunDown);
		modeBox[1].add(cmdDelRun);
		panCmd.add(cmdCancel);
		panCmd.add(cmdSave);
		panCmdMode.add(modeBox[0]);
		panCmdRun.add(modeBox[1]); 
		
		panMode.add(panCmdMode,BorderLayout.WEST);
		panMode.add(modePanel,BorderLayout.EAST);
		panRun.add(panCmdRun, BorderLayout.EAST);
		panRun.add(runPanel,BorderLayout.WEST);
		
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Mode");
		TitledBorder runTitle;
		runTitle = BorderFactory.createTitledBorder("Run");
		panMode.setBorder(title);
		panRun.setBorder(runTitle);
		
		//panel
		panBorder.add(panMode,BorderLayout.WEST);
	    panBorder.add(panRun,BorderLayout.EAST);
	    panBorder.add(panCmd, BorderLayout.SOUTH);

	    
//		centrage
		this.setSize(450,350);
	    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		this.setLocation(
			(screenSize.width - frameSize.width)/2,
			(screenSize.height-frameSize.height)/2
		);
		
		//look and feel
		try {
			UIManager.setLookAndFeel(
					UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}catch (Exception e) {}

		this.setIconImage(new ImageIcon(ICON).getImage());
		this.setResizable(false);
		this.getContentPane().add(panBorder);
		//dialog = new JDialog(this,true);
	}
	
	
	/**
	 * Display a color chooser
	 * @param color
	 * @return
	 */
	public Color getColor(Color color){
		Color c = JColorChooser.showDialog(this,    // Dialog appears over this
                "Pick a Color", // Dialog title 
                color);   // Default color selection
		return c;
	}
	
	/**
	 * Display a file chooser for sound file
	 * @param filePath
	 * @return
	 */
	public String getSound(String filePath){
		JFileChooser fileChoose = new JFileChooser(filePath);
		fileChoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
		
		if (fileChoose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){	
			return fileChoose.getSelectedFile().getAbsolutePath();
		}
		else return "";
	}
	
	public void setDefaultCloseOperation(int operation){
		this.setEnabled(true);
	}
	
	/*
	 * Initialize the list of modes and runs 
	 */
	private void initList() {	    
	
	    modeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    modeList.setFixedCellWidth(100);
	    modeList.setFixedCellHeight(15);
	    modeList.addListSelectionListener(this);	
	    
		
		runList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    runList.setFixedCellWidth(100);
	    runList.setFixedCellHeight(15);
	    runList.addListSelectionListener(this);
	}

	/**
	 * Change listener for list element
	 */
	public void valueChanged(ListSelectionEvent e) {
		if (!e.getValueIsAdjusting()) {
			
			/*
			 * for modes
			 */
			if(modeList.equals(e.getSource())) {
				modeManager.setCurrentMode(modeList.getSelectedIndex());
				modeManager.refreshListRun();
				modeManager.setSelectedIndex();
			 }
			 /*
			  * for runs
			  */
			 if(runList.equals(e.getSource())){
				modeManager.setCurrentRunPack(runList.getSelectedIndex());  
				modeManager.setSelectedIndex();
			 }
	      }
	}
	
	/**
	 * Setter for selected mode list
	 */
	
	public void setModeListIndex(int index){
		modeList.setSelectedIndex(index);
	}
	
	/**
	 * Setter for selected run index
	 * @param index
	 */
	public void setRunListIndex(int index){
		runList.setSelectedIndex(index);
	}

	public int getModeListIndex(){
		return modeList.getSelectedIndex();
	}
	
	/**
	 * Getter for selected run index
	 * @param index
	 */
	public int getRunListIndex(){
		return runList.getSelectedIndex();
	}
	
}
